package hot_100;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class 合并区间 {
    public static void main(String[] args) {

    }

    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (x, y) -> {
            return x[0] - y[0];
        });
        ArrayList<int[]> arr = new ArrayList<>();
        for (int i = 0; i < intervals.length; i++) {
            int sa = arr.size();
            if (sa > 0 && intervals[i][0] <= arr.get(sa - 1)[1]) {
                arr.get(sa - 1)[1] = Math.max(arr.get(sa - 1)[1], intervals[i][1]);
            } else {
                arr.add(intervals[i]);
            }
        }
        return arr.toArray(new int[arr.size()][]);
    }
}
